import Image
import math

OCCUPANCY_THRESHOLD = 150
SENSE_DISTANCE = 100

mojave = Image.open('mojave.tiff')
pixels = mojave.load()

def addt(t1, t2):
    """ Adds individual elements of a tuple to produce a new tuple """
    return t1[0] + t2[0], t1[1] + t2[1]


def subt(t1, t2):
    return t1[0] - t2[0], t1[1] - t2[1]


def occupied(coord):
    if coord[0] < 0 or coord[1] >= mojave.size[1]:
        return True
    pixel = pixels[coord[0], mojave.size[1] - coord[1]]
    if pixel[0] < OCCUPANCY_THRESHOLD:
        return True
    else:
        return False


def soccupied(location, coord):
    if (math.sqrt(abs(location[0] - coord[0]) ** 2 + abs(location[1] - coord[1]) ** 2) > SENSE_DISTANCE):
        return False
    pixel = pixels[coord[0], mojave.size[1] - coord[1]]
    if pixel[0] < OCCUPANCY_THRESHOLD:
        return True
    else:
        return False


def to_screen(coord, sprite):
    return subt(coord, (sprite.width / 2, sprite.height / 2))

def to_coord(coord, sprite):
    return addt(coord, (sprite.width / 2, sprite.height / 2))